package com.cloudz.iptv.utils;

import android.util.Log;

import com.cloudz.iptv.api.ApiConstants;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * @File Name: com.cloudz.iptv.utils
 * @Author: YJ //作者及
 * @Date: 2018-12-17 10:47//完成日期
 * @Description: //  设置拦截器
 * @Version: v0.0.1 // 版本信息
 * @Function List: // 主要函数及其功能
 * @Others: // 其它内容的说明
 * @History: // 历史修改记录
 */
public class InterceptorUtils
{
    public static String TAG = "----";

    /**
     * 日志拦截器
     *
     * @return
     */
    public static HttpLoggingInterceptor LogInterceptor()
    {
        return new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger()
        {
            @Override
            public void log(String message)
            {
                Log.w(TAG, "log: " + message);
            }
        }).setLevel(HttpLoggingInterceptor.Level.BODY);//设置打印数据的级别
    }

    /**
     * 请求头拦截器
     *
     * @return
     */
    public static Interceptor HeaderInterceptor()
    {
        return new Interceptor()
        {
            @Override
            public Response intercept(Chain chain) throws IOException
            {
                Request original = chain.request();
                //在这里你可以做一些想做的事,比如token失效时,重新获取token
                //或者添加header等等,PS我会在下一篇文章总写拦截token方法

                Request.Builder requestBuilder = original.newBuilder();
                if (StringUtils.isNoneBlank(ApiConstants.IPTV_TOKEN))
                {
                    requestBuilder.addHeader("IPTV_TOKEN", ApiConstants.IPTV_TOKEN);
                }
                Request request = requestBuilder.build();

                return chain.proceed(request);
            }
        };
    }
}
